#include "line.hpp"
#include <algorithm>
#include <cmath>

using namespace std;
std::vector<float> interpolate(double i0, double i1, double d0, double d1)
{
    if (i0 == i1) {
        return {static_cast<float>(d0)};
    }
    vector<float> values{};

    double a = (d1 - d0) / (i1 - i0);
    auto d = d0;
    for (double s = i0; s <= i1; s += 1.0) {
        values.push_back(d);
        d += a;
    }

    return values;

}

std::vector<Vector2f> createLine(sf::Vector2f p0, sf::Vector2f p1)
{
    auto dx = std::abs(p1.x - p0.x);
    auto dy = std::abs(p1.y - p0.y);

    std::vector<Vector2f> line;
    // y-axis dominant
    if (dx < dy) {
        // 一定是从小到大
        if (p0.y > p1.y) {
            std::swap(p0, p1);
        }
        auto values = interpolate(p0.y, p1.y, p0.x, p1.x);
        for (float y = p0.y; y < p1.y; y++) {
            line.push_back({values[y - p0.y], y});
        }
    } else {
        if (p0.x > p1.x) {
            std::swap(p0, p1);
        }
        auto values = interpolate(p0.x, p1.x, p0.y, p1.y);
        for (float x = p0.x; x < p1.x; x++) {
            line.push_back({x, values[x - p0.x]});
        }
    }
    return line;
}